/**
 * @file     getKthFromEnd.js
 * @brief    [剑指 Offer 22. 链表中倒数第k个节点](https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/description/)
 * @author   Zhu
 * @date     2023-06-11 21:21
 */

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
var getKthFromEnd = function (head, k) {
    let pre = head;
    let post = head;

    let i = 0;
    while (pre) {
        pre = pre.next;
        if (i >= k) {
            post = post.next;
        }
        i++;
    }

    return post;
};
